// pages/userCenter/userCenter.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: wx.getSystemInfoSync().statusBarHeight,
    showDialog: false,
    phone: '',
    page: 1,
    pageSize: 10,
    isLoading: false,
    noMoreData: false,
    isRefreshing: false,
    items: [],
    currentShop: null,
    selectShop: null,
    nickName: '',
    avatar: '',
  },


  /**
   * 
   */
  loginOut: function () {
    wx.showModal({
      title: '退出登录',
      content: '是否退出登录',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          getApp().saveToken('')
          getApp().saveAccountType('')
          getApp().saveUserInfo(null)
          getApp().savePhone('')
          getApp().saveMerchantStoreInfo(null)
          wx.setStorageSync('token', '')
          wx.setStorageSync('phone', '')
          wx.redirectTo({
            url: '/pages/login/login',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  selectShop: function () {
    wx.showLoading({
      title: '正在获取门店列表',
    })
    this.queryMyMdmMerchantStoreList(true)
    this.setData({
      showDialog: true
    })
  },



  /**
   * 获取门店列表
   */
  queryMyMdmMerchantStoreList: function (refresh = false) {
    var that = this;
    if (this.data.isLoading) return;

    this.setData({
      isLoading: true
    });
    const {
      page,
      pageSize
    } = this.data;
    if (!refresh) {
      wx.showLoading({
        title: '加载中',
      })
    }
    let params = {
      page: page,
      limit: pageSize
    }
    let MerchantStoreInfo = getApp().globalData.MerchantStoreInfo
    getApp().api.queryMyMdmMerchantStoreList(params, {
      success: function (res) {
        if (res.data.rows) {
          let items = res.data.rows
          items = refresh ? items : that.data.items.concat(items),
            items.forEach(e => {
              if (MerchantStoreInfo != null && e.merchantId == MerchantStoreInfo.merchantId) {
                e.check = true
              } else {
                e.check = false
              }
            })
          that.setData({
            items: items,
            noMoreData: res.data.total > items.length ? false : true,
            isRefreshing: false,
            isLoading: false
          })
          wx.hideLoading()
        }
      },
      fail: function (err) {
        that.setData({
          isLoading: false
        });
        wx.hideLoading()
      }
    });
  },

  loadMore() {
    if (this.data.noMoreData || this.data.isRefreshing) return;
    this.setData({
      page: this.data.page + 1
    })
    this.queryMyMdmMerchantStoreList();
  },

  refresh() {
    this.setData({
      isRefreshing: true,
      noMoreData: false,
      page: 1
    });
    this.queryMyMdmMerchantStoreList(true);
  },


  checkShop: function (e) {
    let index = e.currentTarget.dataset.index
    let {
      items,
      selectShop
    } = this.data
    items.forEach((e, i) => {
      if (i == index) {
        if (e.check) {
          e.check = false
          selectShop = null
        } else {
          e.check = true
          selectShop = e
        }
      } else {
        e.check = false
      }
    })
    this.setData({
      selectShop,
      items
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.personalView()
  },

  cancle: function () {
    this.setData({
      showDialog: false
    })
  },

  confirmDialog: function () {
    //TODO 去执行添加
    let {
      selectShop,
      items
    } = this.data
    let hasCheck = false
    items.forEach(e => {
      if (e.check) {
        hasCheck = true
        selectShop = e
      }
    })
    if (hasCheck) {
      this.setData({
        items,
        currentShop: selectShop
      })
      getApp().saveMerchantStoreInfo(selectShop)
    } else {
      getApp().saveMerchantStoreInfo(null)
    }
    this.setData({
      showDialog: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let accountType = getApp().globalData.accountType
    this.setData({
      accountType,
      phone: getApp().globalData.phone
    })
  },

  /**
   * 获取昵称头像
   */
  personalView: function () {
    var that = this;
    let params = {
      source: 6,
    }
    getApp().api.personalView(params, {
      success: function (res) {
        getApp().saveUserInfo(res.data)
        that.setData({
          nickName: res.data.nickname,
          avatar: res.data.avatar
        })
      },
      fail: function (err) {
        wx.hideLoading()
      }
    })

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})